<{include file="public/header.html"}>
<style>
.x-tree-node .x-tree-selected { background-color: #CCCCFF;}
.x-tree-selected-copy {background-color: blue;}
.x-tree-selected-cut  {background-color: red;}
</style>

<script language="javascript">
<!--
var TREE_TASK_PATH = "";
var B9_COM_MSG_PLS_SEL = "请选择";

Ext.QuickTips.init(false); 

if (Ext.form.ComboBox) {
    Ext.apply(Ext.form.ComboBox.prototype, {
        triggerAction:'all',
        selectOnFocus:true,
        editable:false,
        mode:'local',
        lazyInit:false,
        emptyText:B9_COM_MSG_PLS_SEL,
        displayField:'sText',
        anchor:"95%"
    });
}
if (Ext.form.TimeField) {
    Ext.apply(Ext.form.TimeField.prototype, {
        format:'H:i',
        hideLabel:true,
        emptyText:'',
        increment:10,
        maxLength:5,
        anchor:"",
        width:70
   });
}
Ext.override(Ext.form.HtmlEditor, {
    onDisable: function(){
        if(this.rendered){
            var roMask = this.wrap.mask();
            roMask.dom.style.filter = "alpha(opacity=10);"; //IE
            roMask.dom.style.opacity = "0.1"; //Mozilla
            roMask.dom.style.background = "#F0F8FF";
        }
        Ext.form.HtmlEditor.superclass.onDisable.call(this);
    },
    onEnable: function(){
        if(this.rendered){
            this.wrap.unmask();
        }
        Ext.form.HtmlEditor.superclass.onEnable.call(this);
    }
});

var taskTree = new Ext.tree.TreePanel({
    title       : 'Task Tree',
    region      : 'west',
    split       : true,
    width       : 240,
    collapsible : true,
    margins     : '3 0 3 3',
    cmargins    : '1 1 1 1',
    tools       : [{id:'refresh',qtip:'刷新任务树',handler:function() {doReloadTree(true);} }],

    autoScroll:true,
    animate:true,
    enableDD:false,
    containerScroll: true,
    rootVisible: false,
    stateful:false,

    loader:new Ext.tree.TreeLoader({ dataUrl:'taskAjax.php?strAction=treelist' }),
    root: new Ext.tree.AsyncTreeNode({text:'Task Tree',draggable:false,id:'0'}) 
});

var taskForm = new Ext.FormPanel({
    labelAlign: 'right',
    frame :true,
    layout: 'form',
    title : '任务基本信息',
    reader: Ext.data.JsonReader({
        successProperty:"success",
        totalProperty:"total", 
        id: "strTaskID" 
        }, ['strTaskID','strOrderNumb','strPriority','strWbsCode','strTaskName','strTaskType',
            'strPlanStartDate','strPlanStartTime','strPlanFinishDate','strPlanFinishTime',
            'strLimitType','strLimitDate','strLimitTime','strTaskInfo','strFinishPercent',
            'strFactStartDate','strFactStartTime','strFactFinishDate','strFactFinishTime','strFinishInfo',
            'strDuration','strCreateUser','strCreateDate','strUpdateDate'] 
        ),

    items: [{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.3,
            layout:'form',
            items :[{xtype:"numberfield", fieldLabel:"任务号", name:"strOrderNumb",id:"idOrderNumb",
                readOnly:true,style:'background:#F0F8FF;',maxLength:10,width:80},
                {xtype:"hidden",name:"strTaskID",id:'sTaskID',value:''}]
        },{
            columnWidth:.3,
            layout:'form',
            items:[{xtype:"combo",hiddenName:"strPriority",fieldLabel:"优先级",forceSelection:true,
                valueField:'sValue',readOnly:true,style:'background:#F0F8FF;',
                store:new Ext.data.SimpleStore({
                    data:<{ $sImportance }>,
                    fields:['sValue','sText']
                })
            }]
        },{
            columnWidth:.4,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"WBS编号", name:"strWbsCode", readOnly:true,style:'background:#F0F8FF;',
                     maxLength:40,anchor:'96%'}]
        }]
    },{
        xtype:'textfield',fieldLabel:'任务名称',name:'strTaskName',anchor:'96%',vtype:'dblCharField',
        maxLength:200,readOnly:true,style:'background:#F0F8FF;'
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.3,
            layout:'form',
            items :[{xtype:"textfield",fieldLabel: '计划开始时间',name:'strPlanStartDate',anchor:'85%',
                readOnly:true,style:'background:#F0F8FF;'}]
        },{
            columnWidth:.2,
            layout:'form',
            items :[{xtype:"textfield",name:'strPlanStartTime',hideLabel:true,readOnly:true,style:'background:#F0F8FF;',anchor:'80%'}]
        },{
            columnWidth:.3,
            layout:'form',
            items : [{xtype:"textfield",fieldLabel:'计划完成时间',name:'strPlanFinishDate',anchor:'85%',
                readOnly:true,style:'background:#F0F8FF;'}]
        },{
            columnWidth:.2,
            layout:'form',
            items :[{xtype:"textfield",name:'strPlanFinishTime',hideLabel:true,readOnly:true,style:'background:#F0F8FF;',anchor:'80%'}]
        }]
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.3,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"工期(工作日)", name:"strDuration",id:"sDuration",readOnly:true,style:'background:#F0F8FF;',
                     maxLength:10,width:100}]
        },{
            columnWidth:.2,
            layout:'form',
            items : [{xtype:'checkbox',hideLabel:true,boxLabel:'估计工期',checked:false,id:"isEstimate",disabled:true}]
        },{
            columnWidth:.2,
            layout:'form',
            items : [{xtype:'checkbox',hideLabel:true,boxLabel:'自动计算工期',checked:true,disabled:true}]
        },{
            columnWidth:.3,
            layout:'form',
            items:[{xtype:"combo",hiddenName:"strTaskType",fieldLabel:"任务类型",valueField:'sValue',
                value:'',readOnly:true,style:'background:#F0F8FF;',
                store:new Ext.data.SimpleStore({
                    data  :<{ $sTaskType }>,
                    fields:['sValue','sText']
                }) }]
        }]
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.5,
            layout:'form',
            items:[{xtype:"combo",hiddenName:"strLimitType",id:'sLimitType',fieldLabel:"限制类型",forceSelection:true,
                valueField:'sValue',readOnly:true,style:'background:#F0F8FF;',
                store:new Ext.data.SimpleStore({
                    data  :<{ $sTaskLimitType }>,
                    fields:['sValue','sText']
                }) }]
        },{
            columnWidth:.3,
            layout:'form',
            items : [{xtype:"textfield",fieldLabel:'限制日期',name:'strLimitDate',readOnly:true,style:'background:#F0F8FF;',anchor:'85%'}]
        },{
            columnWidth:.2,
            layout:'form',
            items :[{xtype:"textfield",name:'strLimitTime',hideLabel:true,readOnly:true,style:'background:#F0F8FF;',anchor:'80%'}]
        }]
    },{
        xtype:'htmleditor', name:'strTaskInfo',id:'sTaskInfo',
        fieldLabel:'任务详细说明', height:80, width:'98%'
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.5,
            layout:'form',
            items : [{xtype:"combo",hiddenName:"strFinishPercent",fieldLabel:"完成百分比",
                valueField:'sValue',value:'0',
                store:new Ext.data.SimpleStore({
                    data: [ <{ section name="loop_finish" loop=100 step=5 }>
                            ["<{ $smarty.section.loop_finish.index }>","<{ $smarty.section.loop_finish.index }>%"],
                            <{ /section }> ['100','100%']],
                    fields:['sValue','sText']
                }) }]
        },{
            columnWidth:.5,
            layout:'form',
            items : [{xtype:'checkbox',fieldLabel:"里程碑",boxLabel:'标记为里程碑',id:"isMilestone",disabled:true}]
        }]
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.3,
            layout:'form',
            items :[{xtype:"datefield",fieldLabel: '实际开始时间',name:'strFactStartDate',id:'sFactStartDate'}]
        },{
            columnWidth:.2,
            layout:'form',
            items :[{xtype:"timefield",name:'strFactStartTime',id:'sFactStartTime',value:'09:00'}]
        },{
            columnWidth:.3,
            layout:'form',
            items : [{xtype:"datefield",fieldLabel:'实际完成时间',name:'strFactFinishDate',id:'sFactFinishDate'}]
        },{
            columnWidth:.2,
            layout:'form',
            items :[{xtype:"timefield",name:'strFactFinishTime',id:'sFactFinishTime',value:'18:00'}]
        }]
    },{
        xtype:'htmleditor', name:'strFinishInfo',
        fieldLabel:'完成情况说明',value:'',enableSourceEdit:false,
        height:80,width:'98%'
        
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:.36,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"创建者", name:"strCreateUser",readOnly:true,style:'background:#F0F8FF;',anchor:'98%'}]
        },{
            columnWidth:.32,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"创建时间", name:"strCreateDate",readOnly:true,style:'background:#F0F8FF;',anchor:'98%'}]
        },{
            columnWidth:.32,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"最后更改时间", name:"strUpdateDate",anchor:'98%',readOnly:true,style:'background:#F0F8FF;'}]
        }]
    }],

    buttons: [{
            text: '保 存',id:'btnSave',handler:doSaveTask  
        },{
            text: '关 闭',id:'btnCanel',handler:function() { TabUtil.closeActiveTab();}
        }]
});

function loadTask(taskId) {
    taskForm.getForm().load({
        url:'./taskAjax.php', 
        //waitMsg:'正在载入数据...', 
        params:{ strTaskID:taskId,strAction:ACTION_VIEW},

        success : function(form, action){
            Ext.getCmp("isEstimate").setValue(action.result.data.strIsEstimate);
            Ext.getCmp("isMilestone").setValue(action.result.data.strIsMilestone);
            taskForm.setTitle('任务基本信息');

            loadPretask(taskId);
            loadRes(taskId);
            return true;
        },
        failure : function(form, action){
            Ext.Msg.alert('错误', '数据加载失败！错误信息：'+action.response.responseText);
        }
    });
    return false;
}
taskTree.on("click",function(node) {
    if (node!=null && node.id==Ext.getCmp("sTaskID").getValue() && Ext.getCmp("idOrderNumb").getValue()!="") {
        return true; //has exist
    }
    if (loadTask(node.id)) {
        node.select();
        TREE_TASK_PATH = node.getPath();
    }
    //alert("Path:"+TREE_TASK_PATH);
});

function getFDate(dateId) {
    var sDate = Ext.getCmp(dateId).getValue();
    if (sDate != null && sDate != "") {
        return sDate.format('Y-m-d');
    } else {
        return "";
    }
}
function checkTaskForm() {
    var sStartDate = getFDate("sFactStartDate");
    var sFinishDate = getFDate("sFactFinishDate");
    if (sStartDate!="" && sFinishDate!="") {
        sStartDate = sStartDate+" "+Ext.getCmp("sFactStartTime").getValue()+":00";
        sFinishDate = sFinishDate+" "+Ext.getCmp("sFactFinishTime").getValue()+":00";
        if (sStartDate >= sFinishDate) {
            Ext.Msg.alert("错误", "实际开始时间不能比实际完成时间晚");
            return false;
        }
    }
    return true;
}
function doReloadTree(isTree) {
    taskForm.form.reset();
    if (isTree) {
        taskTree.getRootNode().reload();
    }
}
function doSaveTask() {
    var ff = taskForm.getForm();
    var curNode = null;
    if (ff.isValid()) {
        if (!checkTaskForm()) return false;
        ff.submit({
            url     : './taskAjax.php',
            method  : 'POST',
            //waitMsg : '正在保存数据...',
            params  : {strAction:"UPDATE_FINSH"},
            success : function(form, action){
                //Ext.Msg.alert('info', action.response.responseText);
                Ext.Msg.alert('提示', action.result.msg);
                taskTree.getRootNode().reload();
                taskTree.selectPath(TREE_TASK_PATH);
            },
            failure : function(form, action) {
                ///Ext.Msg.alert('错误', action.response.responseText);
                Ext.Msg.alert('错误', '数据保存失败！');
            }
        });
    } else { //form checked is invalid
        Ext.Msg.alert('错误',"填写的数据有错误，请检查");
    }
};

var xgPreTask = new Ext.Panel({
    frame :true,
    region:'center',
    layout:'form',
    title :'该任务的前置任务信息'
});

function loadPretask(taskId) {
    if (taskId=="" || taskId<0) {
        taskId = Ext.getCmp("sTaskID").getValue();
    }
    var tblHeader = '<table width="100%" bgColor="#FFFFFF" border="1" class="table1">'+
        '<thead><tr>'+
        '<th title="前置任务名称">前置任务名称</th>'+
        '<th title="类型">类型</th>'+
        '<th title="延隔时间">延隔时间</th>'+
        '<th title="备注">备注</th>'+
        '</tr></thead>';

    Ext.Ajax.request({
        url : './taskAjax.php',
        params : {strAction:'preTaskList',strTaskID:taskId },
        success: function(response,options ) {
            //Ext.Msg.alert('info', response.responseText);
            var rtnData = Ext.decode(response.responseText);
            var tblBody = "";
            if (rtnData.total > 0) {
                for (var i=0; i<rtnData.data.length; i++) {
                    tblBody += '<tr '+(i % 2 === 0 ? "" : "class=t1")+'>'+
                    '<td>'+rtnData.data[i].strTaskName+'</td>'+
                    '<td align="center">'+rtnData.data[i].strPreTaskType+'</td>'+
                    '<td>'+rtnData.data[i].strStayTime+'</td>'+
                    '<td>'+rtnData.data[i].strRemark+'</td>'+
                    '</tr>';
                }
            }
            if (xgPreTask && xgPreTask.body) {
                xgPreTask.body.update(tblHeader+tblBody+'</table>');
            }
        },
        failure: function(response,options ) {
            Ext.Msg.alert('error',response.responseText);
        }
    });
}

function loadRes(taskId) {
    if (taskId=="" || taskId<0) {
        taskId = Ext.getCmp("sTaskID").getValue();
    }
    var tblHeader = '<table width="100%" bgColor="#FFFFFF" border="1" class="table1">'+
        '<thead><tr>'+
        '<th title="任务资源登录名">资源登录名</th>'+
        '<th title="任务资源名称">资源名称</th>'+
        '<th title="资源使用单位">使用单位</th>'+
        '<th title="备注">备注</th>'+
        '</tr></thead>';

    Ext.Ajax.request({
        url : './taskAjax.php',
        params : {strAction:'resourceList',strTaskID:taskId },
        success: function(response,options ) {
            //Ext.Msg.alert('info', response.responseText);
            var rtnData = Ext.decode(response.responseText);
            var tblBody = "";
            if (rtnData.total > 0) {
                var rd;
                for (var i=0; i<rtnData.data.length; i++) {
                    rd=rtnData.data[i];
                    tblBody += '<tr '+(i % 2 === 0 ? "" : "class=t1")+'>'+
                    '<td>'+(rd.strUserName=='task_res_other'?"无":rd.strUserName)+'</td>'+
                    '<td>'+rd.strTrueName+'</td>'+
                    '<td align="center">'+rd.strUsageUnit+'%</td>'+
                    '<td>'+rd.strRemark+'</td>'+
                    '</tr>';
                }
            }
            if (xgResource && xgResource.body) {
                xgResource.body.update(tblHeader+tblBody+'</table>');
            }
        },
        failure: function(response,options ) {
            Ext.Msg.alert('error',response.responseText);
        }
    });
}

var xgResource = new Ext.Panel({
    frame :true,
    region:'center',
    layout:'form',
    title :'完成该任务的资源安排'
});

var taskInfo = new Ext.TabPanel({
    region    : 'center',
    resizeTabs: false,
    border    : false,
    tabWidth  : 120,
    activeTab : 0,
    defaults  : {autoScroll : true,autoShow :true},
    items     : [{
        title    : '基本信息',
        id       : 'tabBase',
        layout   : 'fit',
        items    : taskForm
     },{
        title    : '前置任务',
        id       : 'tabPreTask',
        layout   : 'fit',
        items    : xgPreTask
     },{ 
        title    : '资源选择',
        id       : 'tabRes',
        layout   : 'fit',
        items    : xgResource
    }]
});

var viewport = new Ext.Viewport({
    layout:'border',
    items : [taskTree,taskInfo]
});

//must active each other
taskInfo.activate('tabRes');
taskInfo.activate('tabPreTask');
taskInfo.activate('tabBase');

<{if $sTaskID}>
loadTask("<{$sTaskID}>");
Ext.getCmp('sTaskInfo').disable();
<{/if}>

//-->
</script>

<br/>
</body>
</html>